<?php

include("header.php");

$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();

$sql = "SELECT * FROM `user_info`
WHERE email = '".$_user."'";
$record = $db->query_first($sql);
$db->close();
?>

<script type="text/javascript">
function form_sub() 
{ 
	var ok = true;
	if(test_null(document.getElementById("email").value)){ 
		if(!test_email(document.getElementById("email").value)) 
		{ 
			document.getElementById("lemail").style.color="Red";
			ok = false; 
		}else{
			document.getElementById("lemail").style.color="Black";
		}
		if(document.getElementById("email").value!=document.getElementById("reemail").value){
			document.getElementById("lreemail").style.color="Red";
			ok = false; 
		}else{
			document.getElementById("lreemail").style.color="Black";
		}
	}else{
		document.getElementById("lemail").style.color="Black";
		document.getElementById("lreemail").style.color="Black";
	}
	if(!test_null(document.getElementById("nom").value)) 
	{ 
		document.getElementById("lnom").style.color="Red";
		ok = false;  
	}else{
		document.getElementById("lnom").style.color="Black";
	}
	if(!test_null(document.getElementById("prenom").value)) 
	{ 
		document.getElementById("lprenom").style.color="Red";
		ok = false;  
	}else{
		document.getElementById("lprenom").style.color="Black";
	}
	if(!IsNum(document.getElementById("zip").value)) 
	{ 
		document.getElementById("lzip").style.color="Red";
		ok = false;  
	}else{
		document.getElementById("lzip").style.color="Black";
	}
	if(!test_null(document.getElementById("tel").value)) 
	{ 
		document.getElementById("ltel").style.color="Red";
		ok = false;  
	}else{
		document.getElementById("ltel").style.color="Black";
	}

	if(test_null(document.getElementById("passwd").value)){ 
		if(!test_password(document.getElementById("passwd").value,document.getElementById("repasswd").value)) 
		{ 
			document.getElementById("lpasswd").style.color="Red";
			document.getElementById("lrepasswd").style.color="Red";
			ok = false;  
		}else{
			document.getElementById("lpasswd").style.color="Black";
			document.getElementById("lrepasswd").style.color="Black";
		}
	}else{
		document.getElementById("lpasswd").style.color="Black";
		document.getElementById("lrepasswd").style.color="Black";
	}

	if(ok){
		document.getElementById("espaceform").submit();
	}
} 
</script>

<form id="espaceform" action="comptemod.php" method="post">
<h1>Modifier votre email</h1>
Email:<?php echo $_user ?><br/>
<label id="lemail">Nouvel email</label>
<input id="email" name="email" type="text"></input>
<label id="lreemail">Nouvel email</label>
<input id="reemail" name="reemail" type="text"></input><br/>

<h1>Modifier votre mot de passe</h1>
<label id="lpasswd">Nouveau mot de passe</label>
<input id="passwd" name="passwd" type="password"></input>
<label id="lrepasswd">Nouveau mot de passe</label>
<input id="repasswd" name="repasswd" type="password"></input><br/>

<h1>Modifier vos données</h1>
Civilité 
<select name="sex">
	<option value="<?=$record["sex"]?>"><?php echo $record["sex"] ?></option>
	<option value="M.">M.</option>
	<option value="Mme">Mme</option>
</select>
<label id="lnom">Nom</label>
<input id="nom" name="nom" type="text" value="<?php echo $record["nom"] ?>"></input>
<label id="lprenom">Prénom</label>
<input id="prenom" name="prenom" type="text" value="<?php echo $record["prenom"] ?>"></input>
<label id="lzip">Code postal</label>
<input id="zip" name="zip" type="text" value="<?php echo $record["zip"] ?>"></input>
<label id="ltel">Tél </label>
<input id="tel" name="tel" type="text" value="<?php echo $record["tel"] ?>"></input><br/>
<input type="button" value="Enregistrer les modifications" onClick="form_sub()"></input>
</form>
